package com.xtrip.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.annotation.Resource;

import com.tcl.platform.database.DatabaseFacade;
import com.tcl.platform.database.query.SimpleQuery;

public class BaseDao<T> {
	@Resource(name = "facade")
	protected DatabaseFacade facade;
	
	public DatabaseFacade getFacade() {
		return facade;
	}

	public void setFacade(DatabaseFacade facade) {
		this.facade = facade;
	}

	public List<T> find(T condition) {
		SimpleQuery query = new SimpleQuery(condition);
		query.enableLike();
		List<T> list = query.list();
		return list;
	}

	public T load(Object id) {
		Class<T> entityClass = getEntityClass();
		return (T)facade.load(entityClass, (Serializable)id);
	}
	public T load(String entityName,Object id) {
		return (T)facade.load(entityName, (Serializable)id);
	}
	public int insert(T entity) {
		return facade.insert(entity);
	}
	public int insert(String entityName,T entity) {
		return facade.insert(entityName,entity);
	}
	public int update(T entity) {
		return facade.update(entity);
	}
	public int update(String entityName,T entity) {
		return facade.update(entityName,entity);
	}
	public int delete(T entity) {
		return facade.delete(entity);
	}
	public int delete(String entityName,T entity) {
		return facade.delete(entityName,entity);
	}
	public int deleteById(Object id) {
		Class<T> entityClass = getEntityClass();
		return facade.delete(entityClass, id);
	}
	public int deleteById(String entityName,Object id) {
		return facade.delete(entityName, id);
	}
	protected Class<T> getEntityClass(){
		Class<T> entityClass = (Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		return entityClass;
	}
	public List queryWithSql(String sql , List params){
		List list = this.getFacade().query(sql, params);
		return list;
	}
}
